package com.hsmw.api.config;

import com.baomidou.mybatisplus.core.handlers.MetaObjectHandler;
import com.baomidou.mybatisplus.extension.plugins.PaginationInterceptor;
import com.baomidou.mybatisplus.extension.plugins.pagination.optimize.JsqlParserCountOptimize;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.reflection.MetaObject;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.time.LocalDateTime;

/**
 * @author WD
 * @version 1.0
 * @description: TODO
 * @date 2021/1/19 16:56
 */
@Configuration
@Slf4j
public class MybatisPlusConfig implements MetaObjectHandler {

	/**
	 * 分页插件
	 */
	@Bean
	public PaginationInterceptor paginationInterceptor() {
		PaginationInterceptor paginationInterceptor = new PaginationInterceptor();
		paginationInterceptor.setCountSqlParser(new JsqlParserCountOptimize(true));

		paginationInterceptor.setDialectType("mysql");
		return paginationInterceptor;
	}

	@Override
	public void insertFill(MetaObject metaObject) {
		log.info("开始填充数据……");
		this.setFieldValByName("createTime", LocalDateTime.now(), metaObject);
	}

	@Override
	public void updateFill(MetaObject metaObject) {
		log.info("开始修改数据……");
		this.setFieldValByName("updateTime", LocalDateTime.now(), metaObject);
	}


}